package com.www.applyword.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.ViewStub;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.HotJobAdapter;
import com.www.applyword.base.BaseFragment;
import com.www.applyword.bean.HotJobBean;
import com.www.applyword.bean.OrderBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.ui.home.HotJobDetailsActivity;
import com.www.applyword.utils.SPUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2019/10/22 0022 15:16
 * version: 1.0
 */
public class LaunchedFragment extends BaseFragment {
    @BindView(R.id.rl_job)
    RecyclerView rlJob;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;

    ViewStub vsEmpty;

    private HotJobAdapter mHotJobAdapter;
    private List<HotJobBean> hotJobBeans = new ArrayList<>();

    private int page = 1;
    private int Rows = 10;

    @Override
    protected int getLayoutResource() {
        return R.layout.launched_fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initDate(true);
        refresh.setOnRefreshListener(refreshLayout -> initDate(true));
        refresh.setOnLoadMoreListener(refreshLayout ->initDate(false));
        vsEmpty =  rootView.findViewById(R.id.vs_empty);
    }

    private void initDate(boolean isRefresh) {
        rlJob.setLayoutManager(new LinearLayoutManager(getActivity()));
        rlJob.setItemAnimator(new DefaultItemAnimator());
        mHotJobAdapter = new HotJobAdapter(getActivity());
        rlJob.setAdapter(mHotJobAdapter);

        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("Page", page + "");
        params.put("Rows", Rows + "");
        params.put("UserID", SPUtil.getString(Constants.USERID));
        if (Constants.Order_Type == 0) {
            params.put("State", 0 + "");
        } else if (Constants.Order_Type == 1) {
            params.put("State", 1 + "");
        } else if (Constants.Order_Type == 2) {
            params.put("State", 2 + "");
        } else {
            params.put("State", 98 + "");
        }


        HttpClient.get(getActivity(), Constants.GETEMPLOYMENTMEORDERLIST, params, new CallBack<List<OrderBean>>() {
            @Override
            public void onSuccess(List<OrderBean> orderBeans) {
                if (isRefresh) {
                    refresh.finishRefresh(true);
                    if (orderBeans.size() == 0) {
                        showEmpty(true, vsEmpty, 0);
                    } else {
                        showEmpty(false, vsEmpty, 0);
                    }
                    mHotJobAdapter.replaceAll(orderBeans, true);
                } else {
                    if (orderBeans.size() > 0) {
                        refresh.finishLoadMore(true);
                        mHotJobAdapter.replaceAll(orderBeans, true);
                    } else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });

        mHotJobAdapter.setOnItemClickListener((viewHolder, view, i) -> startActivity(new Intent(getActivity(), HotJobDetailsActivity.class)
                .putExtra("OrderID", mHotJobAdapter.getItem(i).getOrder_ID())));
    }
}

